/*
 * Copyright (C) 2003, 2004 Rex Ford
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
package GamePlay;
import java.util.*;
import GamePlay.GameObjects.*;
import GamePlay.Collision.*;
public class GameEnv
 {
	private ArrayList[] gameObjects;
	private ArrayList[] collisionObjects;
	public boolean paused = false;
	private final int numLayers = 4;// set the number of layers
	private ArrayList[] objectsToRemove;
	private ArrayList[] shapesToRemove;
	int removeCount=0;
	public UserShip user;
	
	public GameEnv() 
	{
		//make the layers
		gameObjects = new ArrayList[numLayers];
		collisionObjects=new ArrayList[numLayers];
		objectsToRemove=new ArrayList[numLayers];
		shapesToRemove= new ArrayList[numLayers];
		//keep them from being null
		for (int x = 0; x < gameObjects.length; x++) 
		{
			gameObjects[x]=new ArrayList();
			collisionObjects[x]=new ArrayList();
			objectsToRemove[x]=new ArrayList();
			shapesToRemove[x]=new ArrayList();
		}
	}
	
	public void pulse(long time) 
	{
		//tells all objects to pulse
		for (int x = 0; x < gameObjects.length; x++) 
		{
			ArrayList theLayer = gameObjects[x];
			for(int y=0;y<theLayer.size();y++) 
			{
				GameObject theGO = (GameObject)theLayer.get(y);
				theGO.pulse(time);
			}
		}
		for (int x = 0; x < collisionObjects.length-1; x++) 
		{
			ArrayList theLayer = collisionObjects[x];
			for(int y=0;y<theLayer.size();y++) 
			{			
				((CollisionShape) (theLayer.get(y))).checkCollisions(theLayer,y+1);
			}
		}
		if(removeCount>0)
		{
			for(int x=0;x<gameObjects.length;x++)
			{
				gameObjects[x].removeAll(objectsToRemove[x]);
				collisionObjects[x].removeAll(shapesToRemove[x]);
				objectsToRemove[x]=new ArrayList();
				shapesToRemove[x]=new ArrayList();
			}
			
			removeCount=0;
		}
	}
	
	public void removeAllObjects()
	{
		//this will help with memory and garbage collecting
		for(int x = 0; x< gameObjects.length;x++)
		{
			ArrayList theLayer = gameObjects[x];
			for (int y=0;y<theLayer.size();y++)
			{
			//	GameObject g = ((GameObject)(theLayer.get(y)));
				removeGO(((GameObject)(theLayer.get(y))),x);
			}
		}
	}
	
	public void addGO(GameObject obj, int layer) 
	{
		gameObjects[layer].add(obj);
	}
	
	public void addCO(CollisionShape obj, int layer)
	{
		collisionObjects[layer].add(obj);
	}
	
	public void removeGO(GameObject obj, int layer) 
	{
		objectsToRemove[layer].add(obj);
		removeCount+=1;
	}
	public void removeCO(CollisionShape obj,int layer)
	{
		shapesToRemove[layer].add(obj);
		removeCount+=1;
	}
	
	public ArrayList getGOs(int layer) 
	{
		return gameObjects[layer];
	}
	public int getNumLayers() 
	{
		return numLayers;
	}
	
	public UserShip getUser() 
	{
		return user;
	}
	
	public void setUser(UserShip theUser)
	{
		user=theUser;	
	}
	
	public void setPaused(boolean isPaused)
	{
		paused = isPaused;
	}

}